<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bienvenidos a gastobien.com</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<!-- Le styles -->
<link href="css/bootstrap.css" rel="stylesheet">
<style>
body {
	padding-top: 60px;
	/* 60px to make the container go all the way to the bottom of the topbar */
}

.ui-dialog .ui-state-error {
	padding: .3em;
}
</style>
<link href="css/bootstrap-responsive.css" rel="stylesheet">
<link href="css/jquery-ui.css" rel="stylesheet">

<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
      <script src="js/html5shiv.js"></script>
    <![endif]-->
<script src="js/jquery-1.9.1.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/custom.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		showCategories();
	});

	function showCategories() {
		var target = $('#divContainer');
		var data = '';
		$(target).html("<img src='img/ajax-loader.gif'>");
		$.get("showCategories", data, function(rpta) {
			$(target).html(rpta);
		});
	}

	$(function() {
		$('#add_new').click(function() {
			$('#hdnAction').val('new');
			show('new', '');
		});
	});

	$(function() {
		$("#add-new-form").dialog({
			autoOpen : false,
			height : 300,
			width : 350,
			modal : true,
			buttons : {
				"Aceptar" : function() {
					var action = $('#hdnAction').val();
					if (action == 'new') {
						addNewCategory();
						showCategories();
					}
					if (action == 'edit') {
						var categoryId = $('#hdnCategoryId').val();
						updateCategory(categoryId);
						showCategories();
					}
					$(this).dialog("close");
				},
				"Cancelar" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				$(this).dialog("close");
			}
		});
	});	

	function show(action, categoryId) {
		var target = $('#add-new-form');
		if (action == 'edit') {
			$('#hdnCategoryId').val(categoryId);
			var data = 'action=edit&categoryId=' + categoryId;
			$.post("getCategoryById", data, function(rpta) {
				$(target).html(rpta);
				$("#add-new-form").dialog("open");
			});
		}
		if (action == 'new') {
			$(target).load('category.jsp');
			$("#add-new-form").dialog("open");
		}

		$('#hdnAction').val(action);
	}

	function addNewCategory() {
		var target = $('#add-new-form');
		var data = $('#addNewForm').serialize();
		$.post("addNewCategory", data, function(rpta) {
			$(target).html(rpta);
		});
	}

	function updateCategory(categoryId) {
		var target = $('#add-new-form');
		var data = $('#addNewForm').serialize() + '&categoryId=' + categoryId;
		$.post("updateCategory", data, function(rpta) {
			$(target).html(rpta);
		});
	}
</script>
</head>
<body>
	<div id="add-new-form" title="Nueva Categoria"></div>
	<div class="navbar navbar-inverse navbar-fixed-top">
		<div class="navbar-inner">
			<div class="container">
				<button type="button" class="btn btn-navbar" data-toggle="collapse"
					data-target=".nav-collapse">
					<span class="icon-bar"></span> <span class="icon-bar"></span> <span
						class="icon-bar"></span>
				</button>
				<a class="brand" href="home.jsp">gastobien.com</a>
				<div class="nav-collapse collapse">
					<ul class="nav">
						<li class="active"><a href="#">Inicio</a></li>
						<li><a href="#about">Acerca de nosotros</a></li>
						<li><a href="#contact">Contacto</a></li>
						<li><a href="#me">${sessionScope.login}</a></li>
						<li><a href="logout">Salir</a></li>
					</ul>
				</div>
				<!--/.nav-collapse -->
			</div>
		</div>
	</div>
	<div id="divContainer" class="container"></div>
	<div class="container">
		<button id="add_new" class="btn btn-default">Agregar nueva
			categoria</button>
	</div>
	<input id="hdnAction" value="" type="hidden">
	<input id="hdnCategoryId" value="" type="hidden">
	<!-- /container -->
</body>
</html>